package com.itheima.app.manager;

import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.db.BlackList;
import com.itheima.domain.db.Notification;
import com.itheima.domain.db.Question;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.domain.vo.SettingVo;
import com.itheimma.service.db.BlackListService;
import com.itheimma.service.db.NotificationService;
import com.itheimma.service.db.QuestionService;
import com.itheimma.service.db.UserInfoService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class SettingManager {
    @DubboReference
    private QuestionService questionService;

    @DubboReference
    private NotificationService notificationService;

    @DubboReference
    private BlackListService blackListService;

    @DubboReference
    private UserInfoService userInfoService;



    public ResponseEntity findSettingVo(){
        /*
        * 从线程池中获得userId（user包括userID和phone）
        * 把通知和问题取出来，放进去
        * 有或无
        * 返回vo
        * */
        Long id = UserHolder.get().getId();
        Question question = questionService.findById(id);
        Notification notification = notificationService.findById(id);

        SettingVo settingVo = new SettingVo();

        if (question != null){
            settingVo.setStrangerQuestion(question.getStrangerQuestion());
        }
        if (notification != null){
            settingVo.setGonggaoNotification(notification.getGonggaoNotification());
            settingVo.setLikeNotification(notification.getLikeNotification());
            settingVo.setPinglunNotification(notification.getPinglunNotification());
        }

        settingVo.setId(id);
        settingVo.setPhone(UserHolder.get().getPhone());

        return ResponseEntity.ok(settingVo);
    }

    public void setQuestion(String content){
        Long id = UserHolder.get().getId();
        Question question = questionService.findById(id);
        if (question == null){
            //新增
            question = new Question();
            question.setUserId(id);
            question.setStrangerQuestion(content);
            questionService.save(question);
        }else{
            //更新
            question.setStrangerQuestion(content);
            questionService.update(question);
        }
    }

    public void setNotification(Notification param){
        Long id = UserHolder.get().getId();
        Notification notification = notificationService.findById(id);
        if (notification == null){
            param.setUserId(id);
            notificationService.save(param);
        }else{
            notification.setGonggaoNotification(param.getGonggaoNotification());
            notification.setLikeNotification(param.getLikeNotification());
            notification.setPinglunNotification(param.getPinglunNotification());
            notificationService.update(notification);
        }
    }

    public ResponseEntity findBlackListByPage(Integer pageNum , Integer  pageSize){
        Long id = UserHolder.get().getId();
        PageBeanVo pageBeanVo = blackListService.findByPage(id, pageNum, pageSize);
        /*分页查出来的是blackList的id分页信息*/
        List<BlackList> items = (List<BlackList>) pageBeanVo.getItems();
        List<UserInfo> userInfoList = new ArrayList<>();
        /*判断取出来的结果，如果不是空，或者没有值*/
        if (items != null && items.size()>0){
            for (BlackList item : items) {
                Long userId = item.getBlackUserId();
                UserInfo userInfo = userInfoService.findById(userId);
                userInfoList.add(userInfo);
            }
        }
        pageBeanVo.setItems(userInfoList);

        return ResponseEntity.ok(pageBeanVo);
    }

    public void removeBlackList(Long blackUserId){
        Long id = UserHolder.get().getId();
        blackListService.remove(id,blackUserId);
    }
}
